package com.xyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seventh.common.pojo.R;
import com.xyf.mapper.TbMaintainShopMapper;
import com.xyf.pojo.entity.TbMaintainShop;
import com.xyf.pojo.vo.MaintainShopVo;
import com.xyf.service.TbMaintainShopService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
@ApiModel(value = "维修厂实现类" , description = "维修厂实现类")
public class TbMaintainShopServiceImpl implements TbMaintainShopService {

    @Resource
    TbMaintainShopMapper tbMaintainShopMapper;


    @Override
    @ApiModelProperty("分页查询维修厂")
    public R findAllMaintainShopByPage(Integer currentPage, Integer pageSize, MaintainShopVo maintainShopVo) {
        Page<TbMaintainShop> page = new Page<>(currentPage,pageSize);
        QueryWrapper<TbMaintainShop> wrapper=new QueryWrapper<>();

        if (StringUtils.isNotEmpty(maintainShopVo.getName())){
            wrapper.like("name",maintainShopVo.getName());
        }
        if (StringUtils.isNotEmpty(maintainShopVo.getPhoneNum())){
            wrapper.eq("phone_num",maintainShopVo.getPhoneNum());
        }
        if (StringUtils.isNotEmpty(maintainShopVo.getAddress())){
            wrapper.eq("address",maintainShopVo.getAddress());
        }

        Page<TbMaintainShop> page1 = tbMaintainShopMapper.selectPage(page,wrapper);

        return R.ok().put("records",page1.getRecords()).put("total",page1.getTotal());
    }

    @Override
    @ApiModelProperty("根据Id查询")
    public TbMaintainShop selectById(Integer id) {
        TbMaintainShop tbMaintainShop = tbMaintainShopMapper.selectById(id);
        return tbMaintainShop;

    }

    @Override
    @ApiModelProperty("添加维修厂")
    public R insertMaintainShop(TbMaintainShop tbMaintainShop) {
        int i = tbMaintainShopMapper.insert(tbMaintainShop);
        if (i>0){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @Override
    @ApiModelProperty("删除维修厂")
    public R deleteMaintainShop(Integer id) {
        int i = tbMaintainShopMapper.deleteById(id);
        if (i>0){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @Override
    @ApiModelProperty("修改维修厂")
    public R updateMaintainShop(TbMaintainShop tbMaintainShop) {
        int i = tbMaintainShopMapper.updateById(tbMaintainShop);
        if (i>0){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
